package com.faneui.transfer.gateway.config;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.faneui.transfer.gateway.filter.SecurityWebFilter;

/**
 * 
 * @author wangyifeng
 *
 */
@Configuration
public class ShiroConfiguration {

	@Bean(name = "shiroFilter")
	public ShiroFilterFactoryBean getShiroFilterFactoryBean() {

		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
		DefaultWebSecurityManager securityManager = new org.apache.shiro.web.mgt.DefaultWebSecurityManager();
		shiroFilterFactoryBean.setSecurityManager(securityManager);
		shiroFilterFactoryBean.setLoginUrl("/login");
		shiroFilterFactoryBean.setSuccessUrl("/exingcai/captain/userInfo");
		shiroFilterFactoryBean.setUnauthorizedUrl("/403");

		Map<String, Filter> filters = new HashMap<String, Filter>();
		SecurityWebFilter securityWebFilter = new SecurityWebFilter();
		filters.put("securityWebFilter", securityWebFilter);
		shiroFilterFactoryBean.setFilters(filters);

		Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
		filterChainDefinitionMap.put("/login", "authc");
		filterChainDefinitionMap.put("/**", "securityWebFilter");
		shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

		return shiroFilterFactoryBean;
	}
}
